package cn.wolfcode.wolf2world.utils;

import cn.wolfcode.wolf2world.exception.LogicException;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;
import sun.applet.resources.MsgAppletViewer;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.*;

/**
 * @author 杨兴健
 * @Date 2020/8/8 19:01
 */
public class AssertUtil {

    /**
     * 断言非空,如果为空，抛自定义的逻辑异常
     * @param arg 要判断的参数
     * @param msg 要显示异常的信息
     */
    public static void notNull(String arg,String msg) {
        if (!StringUtils.hasLength(arg)) {
            throw new LogicException(msg);
        }
    }

    /**
     * 断言字符串相等
     * @param v1
     * @param v2
     * @param msg
     */
    public static void equal(String v1, String v2, String msg) {
        if (!StringUtils.hasLength(v1)) {
            throw new LogicException(msg);
        }
        if (!Objects.equals(v1,v2)) {
            throw new LogicException(msg);
        }
    }

    /**
     * 手机正则断言
     * @param phone
     * @param msg
     */
    public static void phoneNum(String phone,String msg) {
        Pattern pattern = compile("^((13[0-9])|(15[^4])|(18[0-9])|(17[0-9])|(147))\\d{8}$");
        Matcher matcher = pattern.matcher(phone);
        if (!matcher.matches()) {
            throw new LogicException(msg);
        }
    }
}